<?php

namespace app\controller\api\property;

use think\App;
use app\controller\api\Base;
use app\common\services\CacheService;
use app\validate\property\unit\PropertyUnitValidate;
use app\common\repositories\property\basic\PropertyUnitRepository;

class PropertyUnit extends Base
{
    protected $repository;

    public function __construct(App $app, PropertyUnitRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * 获取楼栋列表
     * @param int $id
     * @return mixed
     *
     * @author Amos
     * 2024-06-12 22:10
     */
    public function topUnitList(int $id)
    {
        $where['property_id'] = $id;
        $topData = $this->repository->getSearch([])
            ->where('pid', 0)
            ->where('is_show', 1)
            ->where('property_id', $id)
            ->field('id,unit_name')
            ->select()->toArray();
        return app('api_return')->success($topData);
    }

    /**
     * 获取楼栋列表
     * @param int $id
     * @return mixed
     *
     * @author Amos
     * 2024-06-12 22:10
     */
    public function unitList(int $id)
    {
        $where['is_show'] = 1;
        $where['property_id'] = $id;
        $res = CacheService::create($this->request->propertyId)
            ->remember('property_unit_' . $this->request->propertyId . $id, function () use ($where) {
                return $this->repository->getApiFormatList($where, $this->request->propertyId);
            }, 86400);
        return app('api_return')->success($res);
    }

    public function add()
    {
        $param = $this->request->param([
            'property_id' => '',
            'unit_name' => '',
            'pid' => '',
            'is_show' => '',
            'remark' => '',
        ]);
        $param['pid'] = (int)$param['pid'];
        validate(PropertyUnitValidate::class)->scene('add')->check($param);
        if ($this->repository->unitNameExists($param['property_id'], $param['unit_name'], $param['pid'])) {
            return $this->error($param['unit_name'] . '已存在');
        }
        $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId());
        return app('api_return')->success('添加成功');
    }

    public function edit(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $param = $this->request->param([
            'unit_name' => '',
            'pid' => '',
            'is_show' => '',
        ]);
        validate(PropertyUnitValidate::class)->scene('edit')->check($param);
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        $this->repository->editInfo($info, $param, $this->request->userId());
        return app('api_return')->success('更新成功');
    }


    public function delete(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        ##  检测出租中的房间
        $res = $this->repository->deleteCheckRoom($id);
        if ($res !== true) {
            return $this->error($res);
        }
        $res = $this->repository->batchDelete([$id], $this->request->userId());
        if ($res) {
            return $this->successText('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }
}